package util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wuyunbin
 */
public class CheckUtil {
    /**
     * 图片地址前缀
     */
    private final String imagePrefix = "https://mashangjie-pro.oss-cn-beijing.aliyuncs.com/";

    /**
     * 正则检查手机号
     *
     * @param phone 手机号
     * @return boolean
     */
    public boolean checkPhone(String phone) {
        if (phone != null && !"".equals(phone)) {
            //非空，进行正则验证
            String regex = "^(1[3-9][0-9])\\d{8}$";
            Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(phone);
            return matcher.matches();
        }
        return false;
    }

    /**
     * 正则检查固话
     *
     * @param telephone 固话号码
     * @return boolean
     */
    public boolean checkTelePhone(String telephone) {
        if (telephone != null && !"".equals(telephone)) {
            //非空，进行正则验证
            String regex = "^[0][1-9]{2,3}-[0-9]{5,10}$";
            Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(telephone);
            return matcher.matches();
        }
        return false;
    }

    /**
     * 正则检查邮箱
     *
     * @param email 邮箱
     * @return boolean
     */
    public boolean checkEmail(String email) {
        if (email != null && !"".equals(email)) {
            //非空，进行正则验证
            String regex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
            Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(email);
            return !matcher.matches();
        }
        return true;
    }

    /**
     * 检查是否是图片
     *
     * @param path
     * @return
     */
    public boolean checkImage(String path){
        return true;
    }


    /**
     * 检查是否都是图片,并去掉共同开头https://bzsc-official.oss-cn-shenzhen.aliyuncs.com
     *
     * @param src
     * @return
     */
    public Map<String, Object> checkImages(String src) {
        String[] split = src.replaceAll(" ", "").split(",");
        String result = "{";
        HashMap<String, Object> map = new HashMap<>(2);
        for (int i = 0; i < split.length; i++) {
            checkImage(split[i]);
            if (split[i].startsWith(imagePrefix)) {
                if (i == split.length - 1) {
                    result += "\"img" + (i + 1) + "\":\"" + split[i].substring(split[i].lastIndexOf("/") + 1) + "\"";
                } else {
                    result += "\"img" + (i + 1) + "\":\"" + split[i].substring(split[i].lastIndexOf("/") + 1) + "\"" + ",";
                }
            } else {
                if (i == split.length - 1) {
                    result += "\"img" + (i + 1) + "\":\"" + split[i] + "\"";
                } else {
                    result += "\"img" + (i + 1) + "\":\"" + split[i] + "\"" + ",";
                }
            }
        }
        result.substring(0, result.length() - 1);
        result += "}";
        System.out.println(result);
        map.put("flag", true);
        map.put("message", result);
        return map;
    }
}
